﻿function NavToPage(pageIndex, getUrl, container, params) {
    var paramsStr = '';
    if (params)
    {
        for (var index in params)
        {
            paramsStr += "&" + params[index].name + "=" + encodeURI(params[index].value);
        }
    }
    $.get(getUrl + "?pageIndex=" + pageIndex + paramsStr, function (result) {
        console.info(result);
        $(container).html(result);
    });
}

function MakerNavs(controller, action, currentPageIndex, pageCount, pageSize, totalItemCount, navContainer, reshContainer, params)
{
    var htmlContainer = $('#' + reshContainer);
    var navContainerObjet = $('#' + navContainer);

    var btns = [];
    //btns.push({ index: 1, text: '1', className: 'pagerBtn pagerBtn-default', bindClickEvent: true });
    //if (currentPageIndex > 1)
    //    btns.push({ index: currentPageIndex - 1, text: '<', className: 'pagerBtn pagerBtn-default', bindClickEvent: true });
    for (var i = currentPageIndex - 2; i <= currentPageIndex + 2; i++) {
        if (i < 1 || i > pageCount) {
            continue;
        }
        if (i == currentPageIndex)
            btns.push({ index: i, text: i + "", className: 'curpagerBtn', bindClickEvent: false });
        else
            btns.push({ index: i, text: i + "", className: 'pagerBtn pagerBtn-default', bindClickEvent: true });
    }

    if (currentPageIndex > 1) {
        btns.splice(0, 0, { index: currentPageIndex - 1, text: '<', className: 'pagerBtn pagerBtn-default', bindClickEvent: true })
    }
    if (currentPageIndex < pageCount) {
        btns.splice(btns.length, 0, { index: currentPageIndex + 1, text: '>', className: 'pagerBtn pagerBtn-default', bindClickEvent: true })
    }

    if (currentPageIndex - 2 > 1)
    {
        btns.splice(0, 0, { index: 1, text: '1...', className: 'pagerBtn pagerBtn-default', bindClickEvent: true })
    }

    if (currentPageIndex + 2 < pageCount) {
        btns.splice(pageCount, 0, { index: pageCount, text: '...' + pageCount, className: 'pagerBtn pagerBtn-default', bindClickEvent: true })
    }

    $(btns).each(function (index,btn)
    {
        var btnElement = $('<button class="' + btn.className + '">' + btn.text + '</button>');
        if (btn.bindClickEvent) {
            btnElement.click(function () {
                if (btn.index != currentPageIndex)
                    NavToPage(btn.index, controller + "/" + action, htmlContainer, params);
            });
        }
        btnElement.appendTo(navContainerObjet);
    });

    $('<span class="skipLabel">转到</span>').appendTo(navContainerObjet);
    var txtElement = $('<input id="pageNo" name="pageNo" class="pagerTxt" type="text">');
    txtElement.keydown(function (event)
    {
        return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode==8;
    });
    txtElement.appendTo(navContainerObjet);

    var skipBtn = $('<button class="pagerBtn pagerBtn-default">确定</button>');
    skipBtn.click(function () {
        var pagerIndex = Number(txtElement.val());
        if (pagerIndex >= 1 && pagerIndex <= pageCount && pagerIndex != currentPageIndex)
        {
            NavToPage(pagerIndex, controller + "/" + action, htmlContainer, params);
        }
    });
    skipBtn.appendTo(navContainerObjet);
    $('<span>(' + (currentPageIndex * pageSize) + '/' + totalItemCount + ')</span>').appendTo(navContainerObjet);
    //navContainerObjet.append('<span class="skipLabel">转到</span>');
    //navContainerObjet.append('<input id="pageNo" name="pageNo" class="pagerTxt" type="text">');
    //navContainerObjet.append('<button class="pagerBtn pagerBtn-default">确定</button>');
}
